<template>
  <div class="imageContainer">
    <el-upload
      v-if="isSelfInput"
      class="avatar-uploader"
      :limit="1"
      accept="image/jpeg,image/gif,image/png,image/bmp"
      :action="uploadUrl"
      :data="uploadData"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
    >
      <div v-if="!imageUrl">
        <i class="el-icon-plus avatar-uploader-icon"/>
      </div>
      <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="图片">
    </el-upload>
    <!--    <el-input v-if="!isSelfInput" v-model="imageUrl" placeholder="请输入图片url" style="width:200px"/>-->
    <!--    <el-button @click="isSelfInput = !isSelfInput">{{ isSelfInput ? '输入' : '上传' }}</el-button>-->
  </div>
</template>

<script>
import { uploadUrl } from '@/api/qiniuyun'

export default {
  name: 'QiniuUpload',
  // eslint-disable-next-line vue/require-prop-types
  props: ['imageUrl'],
  data() {
    return {
      uploadData: { key: '', token: '' },
      isSelfInput: true,
      uploadUrl
    }
  },

  methods: {
    handleAvatarSuccess(res, file, fileList) {
      console.log('上传成功', res, file, fileList)
      this.imageUrl = res.data
      this.$emit('update:imageUrl', res.data)
      this.$emit('success', res.data)
      this.$message.success('上传图片成功')
    }
  }
}
</script>

<style scoped lang="scss">
/* 上传图片*/
.avatar-uploader .el-upload {
  border: 1px dashed #66B1FF;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  border: 1px dashed #66B1FF;

  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.imageContainer {

  .el-button {
  }
}
</style>
